package com.pilipili.zeus.eduService.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pilipili.zeus.eduService.entity.EduTeacher;
import com.pilipili.zeus.eduService.entity.excel.TeacherExcelData;
import com.pilipili.zeus.eduService.entity.vo.Query;
import com.pilipili.zeus.eduService.listener.TeacherListener;
import com.pilipili.zeus.eduService.mapper.EduTeacherMapper;
import com.pilipili.zeus.eduService.service.EduTeacherService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *
 * 作者 服务实现类
 *
 * @author Unlimited_Blade_Works
 * @since 2021-05-21
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {
    @Override
    public void pageQuery(Page<EduTeacher> pageParam, Query query) {

        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");

        if (query == null){
            baseMapper.selectPage(pageParam, queryWrapper);
            return;
        }

        String name = query.getName();
        Integer level = query.getLevel();
        String begin = query.getBegin();
        String end = query.getEnd();

        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }

        if (!StringUtils.isEmpty(level) ) {
            queryWrapper.eq("level", level);
        }

        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create", begin);
        }

        if (!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_create", end);
        }
        queryWrapper.orderByDesc("gmt_create");
        baseMapper.selectPage(pageParam, queryWrapper);
    }

    @Override
    public Map<String, Object> getFrontTeacherList(Page<EduTeacher> page) {
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("level");
        baseMapper.selectPage(page,queryWrapper);

        List<EduTeacher> records = page.getRecords();

        HashMap<String, Object> map = new HashMap<>(20);
        map.put("items", records);
        map.put("current", page.getCurrent());
        map.put("pages", page.getPages());
        map.put("size", page.getSize());
        map.put("total", page.getTotal());
        map.put("hasNext", page.hasNext());
        map.put("hasPrevious", page.hasPrevious());
        return map;
    }

    @Override
    public void saveTeachersDataByExcel(MultipartFile file, EduTeacherService teacherService) {
        try {
            //文件输入流,调用方法读取Excel
            InputStream stream = file.getInputStream();
            EasyExcel.read(stream, TeacherExcelData.class,new TeacherListener(teacherService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
